@page "/Rent/{License}"
@using Rental.Data
@using Rental.Data.Context
@inject RentalContext RentalContext

<Navigation/>

<AuthorizeView>
    <Authorized>

        <section class="row row-cols-1 row-cols-xxl-3 m-2 mx-xxl-0">

            <section class="col p-0 my-3 px-xxl-5">
                <img src="@_car.Image" class="img-fluid px-md-5 px-xxl-0 rounded-3" alt="Vehicle">
                <p class="text-muted small mt-3 text-end px-md-5 px-xxl-0">Photo by @_car.Copyright from Pexels</p>
            </section>

            <section class="col p-0 px-xxl-5">
                <div class="card mt-4 col-md-6 offset-md-3 col-xxl-12 offset-xxl-0">
                    <div class="card-body">
                        <h6 class="fw-bold mb-3">Summary</h6>

                        <label class="form-label col-12 text-end small">
                            @_dateStart.ToString("dddd, MMMM dd - HH:mm tt")
                            <input type="date" @bind="_dateStart" class="form-control my-2" min="2015-01-01" max="2022-12-31">
                        </label>

                        <label class="form-label col-12 text-end small">
                            @_dateEnd.ToString("dddd, MMMM dd - HH:mm tt")
                            <input type="date" @bind="_dateEnd" class="form-control my-2" min="2015-01-01" max="2022-12-31">
                        </label>

                        <h6 class="fw-bold border-top mt-3 pt-3">Charges</h6>
                        <p class="mb-1 small">Economy <span class="float-end">$@_car.Price</span></p>
                        <p class="mb-1 small">Taxes and fees <span class="float-end">--</span></p>

                        <h6 class="fw-bold border-top mt-3 pt-3 mb-0">Total <span class="float-end">$@_car.Price</span></h6>

                        <button type="button" class="btn btn-primary col-12 mt-3 fw-bold" @onclick="@ValidateLoan">Generate Loan</button>
                    </div>
                </div>
            </section>

            <section class="col p-0 px-xxl-5">
                <div class="card mt-4 col-md-6 offset-md-3 col-xxl-12 offset-xxl-0">
                    <div class="card-body">
                        <form class="small">
                            <span class="fw-bold">Saved cards:</span>

                            @foreach (var creditCard in _creditCards)
                            {
                                <CreditCardInfo CreditCard="@creditCard"/>
                            }

                            <span class="fw-bold">Add new card:</span>
                            <div class="form-floating my-3">
                                <input id="rental-card-holder-name" class="form-control" type="text"
                                       placeholder="Bojan Viner">
                                <label for="rental-card-holder-name">Card holder name</label>
                            </div>

                            <div class="row row-cols-3">
                                <div class="col-5">
                                    <div class="form-floating">
                                        <input id="rental-card-number" class="form-control" type="text"
                                               placeholder="5134-5264-4">
                                        <label for="rental-card-number">Card number</label>
                                    </div>
                                </div>
                                <div class="col-4">
                                    <div class="form-floating">
                                        <input id="rental-card-exp-date" class="form-control" type="text"
                                               placeholder="Exp. date">
                                        <label for="rental-card-exp-date">Exp. date</label>
                                    </div>
                                </div>
                                <div class="col-3">
                                    <div class="form-floating">
                                        <input id="rental-card-cvv" class="form-control" type="text" placeholder="CVV">
                                        <label for="rental-card-cvv">CVV</label>
                                    </div>
                                </div>
                            </div>
                            <button class="btn btn-success col-12 mt-3">
                                <b>Add card</b>
                            </button>
                        </form>
                    </div>
                </div>
            </section>
        </section>

    </Authorized>
    <NotAuthorized>
        <MessageNotAuthorized/>
    </NotAuthorized>
</AuthorizeView>


<Footer></Footer>

@code {

    [Parameter]
    public string License { get; set; }

    [CascadingParameter]
    private Task<AuthenticationState> AuthenticationStateTask { get; set; }

    private Car _car;

    private DateTime _dateStart = DateTime.Now;

    private DateTime _dateEnd = DateTime.Now.AddDays(7);

    private List<CreditCard> _creditCards;

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateTask;
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            _car = await RentalContext.GetCarByLicense(License);
            _creditCards = await RentalContext.GetCreditCardsByUsername(user.Identity.Name);
        }
    }

    private void ValidateLoan()
    {
        if (_dateStart > _dateEnd)
        {
            Console.WriteLine("The start date cannot be greater than end date.");
            return;
        }

        Rental.Data.Loan loan = new()
        {
            CarLicense = License,
            HasBeenCanceled = "NO",
            DateEnd = _dateEnd,
            DateStart = _dateStart,
            CreditCardNumber = _creditCards[0].Number,
        };

        if (RentalContext.InsertLoan(loan) > 0)
        {
            Console.WriteLine("Transaction successful");
        }
        else
        {
            Console.WriteLine("Transaction wrong");
        }
    }

}